package com.workflow.action;

import java.util.Set;

import com.base.service.PermissionService;
import com.common.bean.Flowtemplatebranch;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Salesdept;
import com.workflow.bean.Salesdeptdetail;
import com.workflow.service.SalesdeptService;
import com.workflow.service.SalesdeptdetailService;

public class SalesdeptdetailManagerAction extends AbstractActionSupport
		implements Preparable {
	private static final long serialVersionUID = -2488719253044831244L;

	private SalesdeptdetailService salesdeptdetailService;
	
	private SalesdeptService salesdeptService;
	
	private PermissionService permissionService;
	
	private Salesdeptdetail salesdeptdetail;
	
	private Salesdept salesdept;
	
	private String salesno;
	
	private String salesdetailno;
	
	private PagerModel pm;
	
	private Set cardsubtypelist;

	public void prepare() throws Exception {
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddSalesdeptdetail() throws Exception {
		salesdept = salesdeptService.findSalesdept(salesno);
		cardsubtypelist = salesdept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addSalesdeptdetail() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		salesdept = salesdeptService.findSalesdept(salesno);
		salesdeptdetail.setSalesdept(salesdept);
		
		Object obj = null;
		long ruleno = salesdept.getFlowtemplatedetail().getRule().getRuleno();
		String staffcode = personnel.getStaffcode();
		
		obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		if(StringToken.DICT_FLOWBRANCH_YES.equals(salesdept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = salesdept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					salesdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(salesdeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						salesdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
			}
		}
		
		salesdeptdetailService.addSalesdeptdetail(salesdeptdetail);
		
		return SUCCESS;
	}
	
	public String findAllSalesdeptdetail() throws Exception {
		salesdept = salesdeptService.findSalesdept(salesno);
		pm = salesdeptdetailService.findSalesdeptdetailForPager(salesno);
		return SUCCESS;
	}
	
	public String findSalesdeptdetail() throws Exception {
		salesdeptdetail = salesdeptdetailService.findSalesdeptdetail(salesdetailno);
		return SUCCESS;
	}
	
	public String getSalesdeptdetailForUpdate() throws Exception {
		salesdeptdetail = salesdeptdetailService.findSalesdeptdetail(salesdetailno);
		salesdept = salesdeptService.findSalesdept(salesdeptdetail.getSalesdept().getSalesno());
		cardsubtypelist = salesdept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String updateSalesdept() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		salesdept = salesdeptService.findSalesdept(salesdeptdetail.getSalesdept().getSalesno());
		
		Object obj = null;
		long ruleno = salesdept.getFlowtemplatedetail().getRule().getRuleno();
		String staffcode = personnel.getStaffcode();
		
		obj = permissionService.findPersonneltorulelink(ruleno, staffcode);	
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		if(StringToken.DICT_FLOWBRANCH_YES.equals(salesdept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = salesdept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					salesdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(salesdeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						salesdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
			}
		}
		
		salesdeptdetailService.updateSalesdeptdetail(salesdeptdetail);
		
		return SUCCESS;
	}

	/**
	 * @return the cardsubtypelist
	 */
	public Set getCardsubtypelist() {
		return cardsubtypelist;
	}

	/**
	 * @param cardsubtypelist the cardsubtypelist to set
	 */
	public void setCardsubtypelist(Set cardsubtypelist) {
		this.cardsubtypelist = cardsubtypelist;
	}

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the salesdept
	 */
	public Salesdept getSalesdept() {
		return salesdept;
	}

	/**
	 * @param salesdept the salesdept to set
	 */
	public void setSalesdept(Salesdept salesdept) {
		this.salesdept = salesdept;
	}

	/**
	 * @return the salesdeptdetail
	 */
	public Salesdeptdetail getSalesdeptdetail() {
		return salesdeptdetail;
	}

	/**
	 * @param salesdeptdetail the salesdeptdetail to set
	 */
	public void setSalesdeptdetail(Salesdeptdetail salesdeptdetail) {
		this.salesdeptdetail = salesdeptdetail;
	}

	/**
	 * @return the salesdeptdetailService
	 */
	public SalesdeptdetailService getSalesdeptdetailService() {
		return salesdeptdetailService;
	}

	/**
	 * @param salesdeptdetailService the salesdeptdetailService to set
	 */
	public void setSalesdeptdetailService(
			SalesdeptdetailService salesdeptdetailService) {
		this.salesdeptdetailService = salesdeptdetailService;
	}

	/**
	 * @return the salesdeptService
	 */
	public SalesdeptService getSalesdeptService() {
		return salesdeptService;
	}

	/**
	 * @param salesdeptService the salesdeptService to set
	 */
	public void setSalesdeptService(SalesdeptService salesdeptService) {
		this.salesdeptService = salesdeptService;
	}

	/**
	 * @return the salesdetailno
	 */
	public String getSalesdetailno() {
		return salesdetailno;
	}

	/**
	 * @param salesdetailno the salesdetailno to set
	 */
	public void setSalesdetailno(String salesdetailno) {
		this.salesdetailno = salesdetailno;
	}

	/**
	 * @return the salesno
	 */
	public String getSalesno() {
		return salesno;
	}

	/**
	 * @param salesno the salesno to set
	 */
	public void setSalesno(String salesno) {
		this.salesno = salesno;
	}
	
	
}
